package com.ay272.sshupdate.config;


import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;


/**
 * 等同于加载以前的applicationcontext.xml
 * Created by java on 2016/5/24.
 */
@Configuration
@ComponentScan(basePackages ="com.ay272.sshupdate",excludeFilters = {@ComponentScan.Filter(value ={Controller.class})})
@EnableTransactionManagement
@EnableAspectJAutoProxy

public class AppConfig {
    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource bean = new DriverManagerDataSource();
        bean.setUsername("sa");
        bean.setPassword("accp");
        bean.setUrl("jdbc:sqlserver://localhost:1433;databaseName=ssh");
        bean.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        return bean;
    }
    @Bean(autowire = Autowire.BY_NAME)
    public LocalSessionFactoryBean sessionFactory(){
        LocalSessionFactoryBean bean = new LocalSessionFactoryBean();
        Properties properties = new Properties();
        properties.put("hibernate.dialect","org.hibernate.dialect.SQLServer2008Dialect");
        properties.put("hibernate.show_sql",true);
        properties.put("hibernate.show_format",true);
        properties.put("hibernate.hbm2ddl.auto","update");
        bean.setPackagesToScan("com.ay272.sshupdate.entity");
        bean.setHibernateProperties(properties);
        return bean;
    }
    @Bean(autowire = Autowire.BY_NAME)
    public HibernateTransactionManager hibernateTransactionManager(){
        HibernateTransactionManager bean = new HibernateTransactionManager();
        return bean;
    }
}
